package com.awspaas.user.apps.cpq.event;

import java.util.ArrayList;
import java.util.List;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class ProjectFormAfterSave extends ExecuteListener{

	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		try {
			String bindId=pec.getProcessInstance().getId();
			//BO表名
	        String boName = pec.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);
	        if(boName.equals("BO_EU_CPQ_PROJECT_MSG")) {
	        	//判断该项目是否已经生成报价单，如果有则进行更新，没有则新增。
	        	BO data=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG").bindId(bindId).detail();
	        	List<BO> datas=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_GRIDS").bindId(bindId).list();
	        	String sql="select   XTMC,XTMH,SUM(ZJ) as TJG from  BO_EU_CPQ_PROJECT_MSG_GRIDS  where  BINDID=?  GROUP BY XTMH";
	        	String bh=data.getString("XMBH");
	        	BO dataPrice=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_UNIT_PRICE").addQuery("PBINDID=", bindId).detail();
	        	if(null!=dataPrice) {
	        		//更新项目报价汇总表信息
	        		dataPrice.set("GSMC",data.getString("GSMC") );
	        		dataPrice.set("XMBH", data.getString("XMBH"));
	        		dataPrice.set("DDSBJGHJ", data.getString("XMCBYS"));	        			        		
	        		if(datas.size()>0) {
	        			//删除系统汇总价格子表的数据
	        			SDK.getBOAPI().removeByBindId("BO_EU_PROJECT_UNIT_PRICE_GRID", dataPrice.getBindId());
	        			List<RowMap> maps=DBSql.getMaps(sql, new Object[] {bindId});
	        			List<BO> bos=new ArrayList<BO>();
	        			for (RowMap rowMap : maps) {
							String XTMC=rowMap.getString("XTMC");
							String XTMH=rowMap.getString("XTMH");
							String TJG=rowMap.getString("TJG");
							BO bo=new BO();
							bo.set("XTMC", XTMC);
							bo.set("XTMH", XTMH);
							bo.set("JG", TJG);
							bos.add(bo);
						}
	        			SDK.getBOAPI().create("BO_EU_PROJECT_UNIT_PRICE_GRID", bos,  dataPrice.getBindId(), pec.getUserContext().getUID());
	        			SDK.getBOAPI().update("BO_EU_CPQ_PROJECT_UNIT_PRICE", dataPrice);
	        		}
	        	}else {
	        		//创建项目报价汇总表信息
	        		if(datas.size()>0) {
	        			BO pData=new BO();
	        			pData.set("GSMC",data.getString("GSMC") );
	        			pData.set("XMBH", data.getString("XMBH"));
	        			pData.set("DDSBJGHJ", data.getString("XMCBYS"));
	        			pData.set("PBINDID", bindId);
	        			List<RowMap> maps=DBSql.getMaps(sql, new Object[] {bindId});
	        			List<BO> bos=new ArrayList<BO>();
	        			for (RowMap rowMap : maps) {
							String XTMC=rowMap.getString("XTMC");
							String XTMH=rowMap.getString("XTMH");
							String TJG=rowMap.getString("TJG");
							BO bo=new BO();
							bo.set("XTMC", XTMC);
							bo.set("XTMH", XTMH);
							bo.set("JG", TJG);
							bos.add(bo);
						}
	        			ProcessInstance processInstance=SDK.getProcessAPI().createBOProcessInstance("obj_0fd66fb39a044f958a929d580de291d1", pec.getUserContext().getUID(), "");
	        			SDK.getBOAPI().create("BO_EU_CPQ_PROJECT_UNIT_PRICE", pData, processInstance.getId(), pec.getUserContext().getUID());
	        			SDK.getBOAPI().create("BO_EU_PROJECT_UNIT_PRICE_GRID", bos,  processInstance.getId(), pec.getUserContext().getUID());
	        		}
	        		
	        	}
	        	BO dataSystemPrice=SDK.getBOAPI().query("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE").addQuery("PBINDID=", bindId).detail();
	        	if(null!=dataSystemPrice) {
	        		//更新项目成本表信息
	        		dataSystemPrice.set("GSMC",data.getString("GSMC") );
	        		dataSystemPrice.set("XMBH", data.getString("XMBH"));
	        		dataSystemPrice.set("BJRQ", data.getString("BJRQ"));
	        		dataSystemPrice.set("XMLX", data.getString("XMLX"));
	        		dataSystemPrice.set("SXXTBH", data.getString("SXXTBH"));
	        		dataSystemPrice.set("SXXT", data.getString("SXXT"));
	        		dataSystemPrice.set("YSCB", data.getString("YSCB"));
	        		dataSystemPrice.set("XMCBYS", data.getString("XMCBYS"));
	        		if(datas.size()>0) {
	        			//删除项目成本预算清单_模块信息子表的数据
		        		SDK.getBOAPI().removeByBindId("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE_GRIDS", dataSystemPrice.getBindId());
		        		List<BO> bos=new ArrayList<BO>();
		        		for (BO bo : datas) {
	        				BO boGrid=new BO();
							String XTMC=bo.getString("XTMC");
							String MKMC=bo.getString("MKMC");
							String XDXH=bo.getString("XDXH");
							String MKBBH=bo.getString("MKBBH");
							String SL=bo.getString("SL");
							String DW=bo.getString("DW");
							String DJ=bo.getString("DJ");
							String ZJ=bo.getString("ZJ");
							String LB=bo.getString("LB");
							String XTMH=bo.getString("XTMH");
							String VERSION=bo.getString("VERSION");
							boGrid.set("XTMC",XTMC );
							boGrid.set("MKMC", MKMC);
							boGrid.set("XDXH", XDXH);
							boGrid.set("MKBBH", MKBBH);
							boGrid.set("SL", SL);
							boGrid.set("DW", DW);
							boGrid.set("DJ", DJ);
							boGrid.set("ZJ", ZJ);
							boGrid.set("LB", LB);
							boGrid.set("XTMH", XTMH);
							boGrid.set("VERSION", VERSION);
							bos.add(boGrid);
						}
		        		SDK.getBOAPI().create("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE_GRIDS", bos,  dataSystemPrice.getBindId(), pec.getUserContext().getUID());
	        			SDK.getBOAPI().update("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE", dataSystemPrice);
	        		}
	        	}else {
	        		//创建项目成本表信息
	        		if(datas.size()>0) {
	        			BO pData=new BO();
	        			pData.set("GSMC",data.getString("GSMC") );
	        			pData.set("XMBH", data.getString("XMBH"));
	        			pData.set("BJRQ", data.getString("BJRQ"));
	        			pData.set("XMLX", data.getString("XMLX"));
	        			pData.set("SXXTBH", data.getString("SXXTBH"));
	        			pData.set("SXXT", data.getString("SXXT"));
	        			pData.set("YSCB", data.getString("YSCB"));
	        			pData.set("XMCBYS", data.getString("XMCBYS"));
	        			pData.set("PBINDID", bindId);
	        			List<BO> bos=new ArrayList<BO>();
		        		for (BO bo : datas) {
	        				BO boGrid=new BO();
							String XTMC=bo.getString("XTMC");
							String MKMC=bo.getString("MKMC");
							String XDXH=bo.getString("XDXH");
							String MKBBH=bo.getString("MKBBH");
							String SL=bo.getString("SL");
							String DW=bo.getString("DW");
							String DJ=bo.getString("DJ");
							String ZJ=bo.getString("ZJ");
							String LB=bo.getString("LB");
							String XTMH=bo.getString("XTMH");
							String VERSION=bo.getString("VERSION");
							boGrid.set("XTMC",XTMC );
							boGrid.set("MKMC", MKMC);
							boGrid.set("XDXH", XDXH);
							boGrid.set("MKBBH", MKBBH);
							boGrid.set("SL", SL);
							boGrid.set("DW", DW);
							boGrid.set("DJ", DJ);
							boGrid.set("ZJ", ZJ);
							boGrid.set("LB", LB);
							boGrid.set("XTMH", XTMH);
							boGrid.set("VERSION", VERSION);
							bos.add(boGrid);
						}
		        		ProcessInstance processInstance=SDK.getProcessAPI().createBOProcessInstance("obj_478c5672750c4e0aadb7d5fbc7fdcb9a", pec.getUserContext().getUID(), "");
	        			SDK.getBOAPI().create("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE", pData, processInstance.getId(), pec.getUserContext().getUID());
	        			SDK.getBOAPI().create("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE_GRIDS", bos,  processInstance.getId(), pec.getUserContext().getUID());
	        		}
	        	}
	        }
		} catch (Exception e) {
			throw e;
		}
		
	}

}
